"""
@author: 江同学呀
@file: text_field.py
@date: 2023/8/22 19:45
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/text.html#text-field-type

    The text family, including text and match_only_text. Analyzed, unstructured text.
    文本系列，包括 text 和 match_only_text 。经过分析的非结构化文本。
"""
from abc import ABC
from typing import Type, Union, List, Dict

from espc.common.field_common import IndexOptions, Similarity, TermVector
from espc.orm.model.base.base import _Base
from espc.orm.model.struct.fielddata_frequency_filter import FieldDataFrequencyFilter
from espc.orm.model.struct.index_prefixes import IndexPrefixes
from espc.orm.model.mapping.field.base_field.base_string_field import _BaseStringField
from espc.orm.model.mapping.field.field_attribute import BoostAttribute, EagerGlobalOrdinalsAttribute, FieldsAttribute, \
    IndexAttribute, IndexOptionsAttribute, NormsAttribute, StoreAttribute, SimilarityAttribute, MetaAttribute, \
    FielddataAttribute, AnalyzerAttribute, FielddataFrequencyFilterAttribute, IndexPrefixesAttribute, \
    IndexPhrasesAttribute, PositionIncrementGapAttribute, SearchAnalyzerAttribute, SearchQuoteAnalyzerAttribute, \
    TermVectorAttribute
from espc.utils.build_add_attr import build_add_attr


class TextField(
    _BaseStringField, AnalyzerAttribute, BoostAttribute, EagerGlobalOrdinalsAttribute, FielddataAttribute,
    FielddataFrequencyFilterAttribute, FieldsAttribute, IndexAttribute, IndexOptionsAttribute, IndexPrefixesAttribute,
    IndexPhrasesAttribute, NormsAttribute, PositionIncrementGapAttribute, StoreAttribute, SearchAnalyzerAttribute,
    SearchQuoteAnalyzerAttribute, SimilarityAttribute, TermVectorAttribute, MetaAttribute, ABC
):
    """
    文本 字段
    todo analyzer类编写，参数用类
    todo analyzer类编写，参数用类
    """
    _field_type: str = "text"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None, analyzer: str = None,
            position_increment_gap: int = None, eager_global_ordinals: bool = None, fielddata: bool = None,
            boost: float = None, norms: bool = None,
            fielddata_frequency_filter: Union[Dict[str, Union[int, float]], FieldDataFrequencyFilter] = None,
            fields: Union[_BaseStringField, List[_BaseStringField]] = None, index_options: Union[str, IndexOptions] = None,
            index_prefixes: Union[Dict[str, float], IndexPrefixes] = None, term_vector: Union[str, TermVector] = None,
            index_phrases: bool = None, store: bool = None, search_analyzer: str = None, meta: Dict = None,
            search_quote_analyzer: str = None, similarity: Union[str, Similarity] = None, index: bool = None
    ):
        super(TextField, self).__init__(
            field_name=field_name, value=value, default=default, boost=boost,
            eager_global_ordinals=eager_global_ordinals, fields=fields, analyzer=analyzer,
            position_increment_gap=position_increment_gap, fielddata=fielddata, norms=norms,
            fielddata_frequency_filter=fielddata_frequency_filter, index_options=index_options, similarity=similarity,
            index_prefixes=index_prefixes, term_vector=term_vector, index_phrases=index_phrases, store=store,
            search_analyzer=search_analyzer, meta=meta, search_quote_analyzer=search_quote_analyzer, index=index
        )
        return

    def __get__(self, instance: _Base, owner: Type["TextField"]) -> Union["TextField", str]:
        return super(TextField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(TextField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(TextField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


class MatchOnlyTextField(_BaseStringField, FieldsAttribute, MetaAttribute, ABC):
    """
    仅匹配文本 字段
    """
    _field_type: str = "match_only_text"

    def __init__(
            self, field_name: str = None, value: str = None, default: str = None,
            fields: Union[_BaseStringField, List[_BaseStringField]] = None, meta: Dict = None
    ):
        super(MatchOnlyTextField, self).__init__(
            field_name=field_name, value=value, default=default, fields=fields, meta=meta)
        return

    def __get__(self, instance: _Base, owner: Type["MatchOnlyTextField"]) -> Union["MatchOnlyTextField", str]:
        return super(MatchOnlyTextField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: str):
        super(MatchOnlyTextField, self).__set__(instance=instance, value=value)
        return

    def __getattribute__(self, item):
        return super(MatchOnlyTextField, self).__getattribute__(item)

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


